home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-02 / openfil2.zip / OPENFILE.PAS < prev   
Pascal/Delphi Source File  |  1991-10-29  |  5KB  |  163 lines

  1. (*
  2.  
  3. OPENFILES - Print list of all open files
  4.  
  5. Written by D.J. Murdoch for the public domain.
  6.  
  7. This unit interfaces one routine, which looks in the (undocumented) DOS list of
  8. open files for the filenames, and prints a list of them.  It automatically
  9. installs an exit handler to call this routine, so if your program bombs because
  10. it runs out of file handles, you'll see the list of what's open.
  11.  
  12. I've tested this unit in MSDOS 3.2 and 4.01; it should work in the other
  13. versions from 2 to 5, but I'd like to hear from you if it doesn't.
  14.  
  15. Fidonet:   DJ Murdoch at 1:163/140.3
  16. Internet:  dmurdoch@watstat.waterloo.edu
  17. CIS:       71631,122
  18.  
  19. History:
  20.   1. 21 Oct 91  - First release to PDN echo.
  21.   2. 26 Oct 91  - Added check of PSP segment, and DOS 3.0 record format.
  22.                   Set Allfiles to true to get previous behaviour.
  23.  
  24.  
  25. *)
  26.  
  27.  
  28. unit openfiles;
  29.  
  30. { Print list of all open files }
  31.  
  32. { Written by D.J. Murdoch for the public domain }
  33.  
  34. interface
  35.  
  36. const
  37.   Allfiles : boolean = false;               { Whether to print files belonging
  38.                                               to other processes }
  39.  
  40. procedure print_open_files(var where:text);
  41.  
  42. implementation
  43.  
  44. uses
  45.   dos;
  46.  
  47. type
  48.   dos2openfilerec = record
  49.     numhandles : byte;
  50.     junk1 : array[1..3] of byte;
  51.     filename : array[4..$e] of char;
  52.     junk2 : array[$f..$27] of byte;
  53.   end;
  54.  
  55.   dos30openfilerec = record                   {!!2}
  56.     numhandles : word;                        {!!2}
  57.     junk1 : array[2..$20] of byte;            {!!2}
  58.     filename : array[$21..$2b] of char;       {!!2}
  59.     junk2 : array[$2c..$31] of byte;          {!!2}
  60.     pspseg : word;                            {!!2}
  61.     junk3 : array[$34..$37] of byte;          {!!2}
  62.   end;
  63.  
  64.   dos3openfilerec = record
  65.     numhandles : word;
  66.     junk1 : array[2..$1f] of byte;
  67.     filename : array[$20..$2a] of char;
  68.     junk2 : array[$2b..$30] of byte;          {!!2}
  69.     pspseg : word;                            {!!2}
  70.     junk3 : array[$33..$34] of byte;          {!!2}
  71.   end;
  72.  
  73.   dos4openfilerec = record
  74.     numhandles : word;
  75.     junk1 : array[2..$1f] of byte;
  76.     filename : array[$20..$2a] of char;
  77.     junk2 : array[$2b..$30] of byte;         {!!2}
  78.     pspseg : word;                           {!!2}
  79.     junk3 : array[$33..$3a] of byte;         {!!2}
  80.   end;
  81.  
  82.   filelistptr = ^filelistrec;
  83.   filelistrec = record
  84.     next : filelistptr;
  85.     numfiles : word;
  86.     case byte of
  87.     2 : (dos2files : array[1..1] of dos2openfilerec);
  88.    30 : (dos30files: array[1..1] of dos30openfilerec);  {!!2}
  89.     3 : (dos3files : array[1..1] of dos3openfilerec);
  90.     4 : (dos4files : array[1..1] of dos4openfilerec);
  91.   end;
  92.  
  93. procedure print_open_files(var where:text);
  94. var
  95.   r : registers;
  96.   list : filelistptr;
  97.   list1 : filelistptr;
  98.   i : word;
  99. begin
  100.   with r do
  101.   begin
  102.     ah := $52;
  103.     msdos(r);
  104.  
  105.     list := pointer(MemL[es:bx+4]);
  106.  
  107.     while ofs(list^) <> $FFFF do
  108.     begin
  109.       with list^ do
  110.         for i:=1 to numfiles do
  111.           case lo(dosversion) of
  112.           2 : with dos2files[i] do
  113.                if numhandles > 0 then
  114.                  writeln(where,filename);
  115.           3 : if hi(dosversion) = 0 then                            {!!2}
  116.               begin                                                 {!!2}
  117.                 with dos30files[i] do                               {!!2}
  118.                  if (numhandles > 0) and (allfiles or               {!!2}
  119.                                           (pspseg = prefixseg)) then{!!2}
  120.                    writeln(where,filename)                          {!!2}
  121.               end                                                   {!!2}
  122.               else                                                  {!!2}
  123.                 with dos3files[i] do
  124.                  if (numhandles > 0) and (allfiles or
  125.                                           (pspseg = prefixseg)) then{!!2}
  126.                    writeln(where,filename);
  127.         4,5 : with dos4files[i] do
  128.                if (numhandles > 0) and (allfiles or                 {!!2}
  129.                                         (pspseg = prefixseg)) then  {!!2}
  130.                  writeln(where,filename);
  131.           end;
  132.       list := list^.next;
  133.     end;
  134.   end;
  135. end;
  136.  
  137. var
  138.   exit_save : pointer;
  139.  
  140. procedure my_exit_proc; far;
  141. var
  142.   junk : word;
  143. begin
  144.   ExitProc := Exit_save;
  145.   junk := ioresult;
  146.   assign(output,'');
  147.   rewrite(output);
  148.   writeln('Files open as program terminates:');
  149.   print_open_files(output);
  150. end;
  151.  
  152. begin
  153.   if not (lo(dosversion) in [2..5]) then
  154.     writeln('OPENFILES only works with DOS 2 to 5')
  155.   else
  156.   begin
  157.     exit_save := ExitProc;
  158.     ExitProc := @my_exit_proc;
  159.   end;
  160. end.
  161.  
  162.  
  163.